package net.asgarli.abalone.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * The part of the unit to be included in the preview
 * 
 * @author Elchin
 */
public class UnitPreview implements Marshallable {
    private static final long serialVersionUID = 1L;

    private Long id;

    private String name;

    private byte[] previewIcon;

    @Override
    public byte[] writeToBytes() throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(id);
        dos.writeUTF(name);
        dos.write(previewIcon);
        return bos.toByteArray();
    }

    @Override
    public void restoreFromBytes(byte[] bytes) throws IOException {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
        id = in.readLong();
        name = in.readUTF();
        in.read(previewIcon);
    }

    //
    // ************************* GENERATED *************************
    // ************************* Getters and Setters ***************
    //
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getPreviewIcon() {
        return previewIcon;
    }

    public void setPreviewIcon(byte[] previewIcon) {
        this.previewIcon = previewIcon;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + Arrays.hashCode(previewIcon);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        UnitPreview other = (UnitPreview) obj;
        if (id == null) {
            if (other.id != null) return false;
        } else if (!id.equals(other.id)) return false;
        if (name == null) {
            if (other.name != null) return false;
        } else if (!name.equals(other.name)) return false;
        if (!Arrays.equals(previewIcon, other.previewIcon)) return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("UnitPreview [id=");
        builder.append(id);
        builder.append(", name=");
        builder.append(name);
        builder.append(", previewIcon=");
        builder.append(Arrays.toString(previewIcon));
        builder.append("]");
        return builder.toString();
    }
}
